package io.renren.modules.study.service.impl;

import io.renren.common.utils.R;
import io.renren.modules.study.dao.IntergralDao;
import io.renren.modules.study.dao.MyCourseDao;
import io.renren.modules.study.dao.StudentDao;
import io.renren.modules.sys.dao.UserDao;
import io.renren.modules.study.entity.IntergralHistory;
import io.renren.modules.study.entity.StudentEntity;
import io.renren.modules.sys.entity.UserEntity;
import io.renren.modules.study.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentDao studentDao;
    @Autowired
    UserDao userDao;
    @Autowired
    IntergralDao intergralDao;
    @Autowired
    MyCourseDao myCourseDao;

    @Override
    public StudentEntity getStudentByPhone(String phone) {
        return studentDao.getStudentByPhone(phone);
    }

    @Override
    public R  authentication(Long userId) {
        //1根据userid取得phoen
        UserEntity userEntity=userDao.getUserByUserId(userId);
        //2根据phone判断是否为认证的学生
        StudentEntity studentEntity=studentDao.getStudentByPhone(userEntity.getMobile());
        //3.如果该学员已认证通过
        if(userEntity.getIsAuthentication()==1){
            return R.ok("您已经认证过了",101);
        }else {
            if (studentEntity != null) {//3是认证学生，修改认证状态，给用户增加积分。将userid绑定到学生表。
                Integer intergral = intergralDao.getIntergralByType(studentEntity.getLearnType());
                Integer balance = intergral + userEntity.getIntegral();

                IntergralHistory ih = new IntergralHistory();
                ih.setBeforeIntegral(userEntity.getIntegral());
                ih.setAfterIntegral(balance);
                ih.setUserId(Math.toIntExact(userId));
                ih.setIntergral(intergral);
                ih.setType(0);//0代表增加
                ih.setUsername(userEntity.getUsername());
                ih.setUsername(userEntity.getUsername());
                ih.setName(studentEntity.getName());
                ih.setPhone(studentEntity.getPhone());
                ih.setRemarks("完成学员认证，赠送" + intergral + "积分。");
                userEntity.setIsAuthentication(1);//说明次用户注释过了
                userEntity.setIntegral(balance);//增加用户积分余额
                studentEntity.setUserId(Math.toIntExact(userId));

                //获取我班级的课程
               // List<MyCourseEntity> myCourseEntities= myCourseDao.getMyClassCourseByUserId(userId);
                //将我的班级的课程保存到我的课程
                userDao.updateAuthentication(userEntity);
                studentDao.setUserId(studentEntity);

                intergralDao.savaIntergralHistory(ih);
            }else{
                return R.ok("认证失败，请确认手机号存在",102);
            }
        }
        //4不是认证学生，返回查无此学生的信息。

        return R.ok("认证成功，积分已到账",100);
    }

    /**
     * 修改学生身份信息
     *
     * @return
     */
    @Override
    public int updateStudentMessage(StudentEntity student) {
        return studentDao.updateStudentMessage(student);
    }

    @Override
    public StudentEntity selectStudentIsNull(int userId) {
        return studentDao.selectStudentIsNull(userId);
    }
}
